import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:quick_widget/drop_menu_container.dart';
import 'package:saas_order_meal/saas_order_meal.dart';
import 'package:saas_order_meal/ui/request_day_menu_widget.dart';
import 'package:saas_order_meal/viewmodel/regular_menu_contain_view_model.dart';

import '../model/order_request_data.dart';
import '../net/net_response/elder_month_record.dart';
import '../res/resources.dart';
import '../viewmodel/by_week_food_menu_view_model.dart';
import '../widget/select_item_widget.dart';

class ByWeekFoodMenuPage extends BaseStatefulMvvmWidget {
  final OrderRequestData orderData;

  ByWeekFoodMenuPage(this.orderData);

  @override
  State<StatefulWidget> createState() => _ByWeekFoodMenuState();
}

class _ByWeekFoodMenuState
    extends BaseMvvmState<ByWeekFoodMenuViewModel, ByWeekFoodMenuPage> {
  late ScrollController scrollController;
  late PageController pageController;

  @override
  void initState() {
    scrollController = ScrollController(
        initialScrollOffset: (widget.orderData.weekDay - 1) * 120.0);
    super.initState();
  }

  @override
  Widget? buildLoadingContentView() {
    pageController = PageController(initialPage: viewModel.weekIndex);
    return Column(
      children: [
        const SizedBox(
          height: 10,
        ),

        ///顶部日期
        SelectItemWidget<ElderMonthRecordList>(
          viewModel.weekData,
          controller: scrollController,
          itemBuilder: (context, data, index, selected) {
            var date = data.date!.split(" ");
            var format = ['m', '月', 'd', '日'];
            return Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                CommonText(date[1],
                    textColor:
                    selected ? Colors.white : Colors.black),
                CommonText(
                    DateTime.parse(date[0]).toDateStr(format),
                    textColor:
                    selected ? Colors.white : Colors.grey[400]!,
                    textSize: 14.5)
              ],
            ).addToContainer(
              width: 110,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: selected
                      ? SaasOrderMeal.mainColor
                      : Colors.white,
                  borderRadius: 6.borderRadius),
              padding:
              EdgeInsets.symmetric(horizontal: 22, vertical: 5),
            );
          },
          scrollDirection: Axis.horizontal,
          position: viewModel.weekIndex,
          separatorBuilder: (context, index) {
            return Container(
              width: 10,
            );
          },
          padding: EdgeInsets.symmetric(horizontal: 16),
          onCallback: (index, data) {
            setState(() {
              viewModel.weekIndex = index;
              pageController.jumpToPage(index);
              viewModel.initHasOrdered();
            });
          },
        ).addToContainer(
          width: double.infinity,
          height: 50,
        ),
        const SizedBox(
          height: 10,
        ),

        ///菜单内容
        // listContentWidget(
        //     viewModel.weekData.map((e) {
        //       return RequestDayMenuWidget(orderData: widget.orderData,orderDate: e.date!.split(" ")[0],);
        //     }).toList(),
        //     index: viewModel.weekIndex).setWeight(1)
        PageView(
          controller: pageController,
          physics: const NeverScrollableScrollPhysics(),
          children: viewModel.weekData.map((e) {
            return RequestDayMenuWidget(
              orderData: widget.orderData,
              orderDate: e.date!.split(" ")[0],
              selectListener: viewModel.listener,
            );
          }).toList(),
        ).setWeight(1)
      ],
    );
  }

  Widget bottomPriceWidget({bool clickShopcar = true}) {
    return Stack(
      children: [
        Image.asset(
          "images/shop_car.png",
          width: 40,
          height: 40,
          package: "saas_order_meal",
          color: viewModel.selectDayFood.isEmpty
              ? Colors.black
              : SaasOrderMeal.mainColor,
        ).onTap(() {
          if (viewModel.selectDayFood.isEmpty) {
            return;
          }
          // if (!clickShopcar) {
          //   Go().pop();
          //   return;
          // }
          if (viewModel.controller.isShow) {
            viewModel.controller.hide();
          } else {
            viewModel.controller
                .showDropMenu(menuWidget: viewModel.bottomContent());
          }
          // showSelectBottom();
        }).setLocation(top: 0, bottom: 0, left: 16),
        if (viewModel.selectDayFood.isNotEmpty)
          CommonText("${viewModel.totalNum}",
                  textColor: Colors.white, textSize: 14)
              .addToContainer(
                  decoration: BoxDecoration(
                      color: Colors.red, borderRadius: 20.borderRadius),
                  alignment: Alignment.center,
                  constraints: BoxConstraints(minHeight: 20, minWidth: 20))
              .setLocation(left: 44, top: 8),
        (Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            CommonText("¥", textSize: 16),
            CommonText((viewModel.totalPrice).toString(), textSize: 20)
          ],
        )).setLocation(top: 0, bottom: 0, left: 70),
        TextButton(
          onPressed: () {
            ///todo:去下单支付
            viewModel.toApplySubmit();
          },
          child: CommonText('去结算', textColor: Colors.white),
          style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(
                  viewModel.selectDayFood.isEmpty
                      ? Colors.grey
                      : SaasOrderMeal.mainColor),
              shape: MaterialStateProperty.all(StadiumBorder()),
              fixedSize: MaterialStateProperty.all(Size(90, 40))),
        )
            .addToContainer(alignment: Alignment.center)
            .setLocation(right: 16, top: 0, bottom: 0)
      ],
    ).addToContainer(width: double.infinity, height: 65, color: Colors.white);
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: SaasOrderMeal.bgColor,
      appBar: AppBar(
        title: Text('周菜单'),
      ),
      body: vm==null?Container(): Column(
        children: [
          DropMenuContainer(
              isBottomOut: true,
              controller: viewModel.controller,
              child: loadingContentWidget)
              .setWeight(1),
          bottomPriceWidget()
        ],
      ),
    );
  }

  @override
  ByWeekFoodMenuViewModel createViewModel() {
    return ByWeekFoodMenuViewModel(context, orderData: widget.orderData);
  }

  @override
  void onRetryClick() {
    viewModel.getMonthData();
  }

}
